import socket

# 1. 创建服务端 socket 对象
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

# 2. 绑定服务的IP:端口
serverSocket.bind(('127.0.0.1',9999))

# 3. 设置监听
serverSocket.listen(5)

# 4. 创建与客户端被动连接
while True:
    conn_socket,addr = serverSocket.accept()
    print(f'客户端连接信息: {addr}')

    # 5. 发送信息给客户端
    msg = 'Welcom to study socket!'
    conn_socket.send(msg.encode(encoding='utf-8'))

    # 6. 接收客户端发送的信息
    msg_c = conn_socket.recv(1024)
    print(msg_c.decode())

    # 7. 关闭与该客户端的连接
    conn_socket.close()